<template>
  <HomePanel title="热门品牌" sub-title="国际经典 品质保证">
    <template v-slot:right>
      <a
        href="javascript:;"
        class="iconfont icon-angle-left prev"
        @click="toggle(-1)"
        :class="{ disabled: currentInd === 0 }"
      ></a>
      <a
        href="javascript:;"
        class="iconfont icon-angle-right next"
        :class="{ disabled: currentInd === 1 }"
        @click="toggle(1)"
      ></a>
    </template>
    <div class="box" ref="box">
      <transition name="fade">
        <ul
          v-if="goods.length"
          class="list"
          :style="{ transform: `translate(${-currentInd * 1240}px)` }"
        >
          <li v-for="i in goods" :key="i.id">
            <RouterLink to="/">
              <img :src="i.picture" alt="" />
            </RouterLink>
          </li>
        </ul>
      </transition>

      <!-- 骨架屏 -->
      <transition name="fade">
        <div v-if="!goods.length">
          <XtxSkeleton
            class="brandSke"
            height="305px"
            width="240px"
            v-for="i in 5"
            :key="i"
          ></XtxSkeleton>
        </div>
      </transition>
    </div>
  </HomePanel>
</template>

<script>
import { ref } from 'vue-demi'
import HomePanel from './Home-panel'
import { lazyLoad } from '@/hooks'
import { useStore } from 'vuex'
export default {
  name: 'HomeBrand',
  components: { HomePanel },
  setup () {
    const currentInd = ref(0)
    function toggle (step) {
      const newVal = currentInd.value + step
      if (newVal > 1 || newVal < 0) {
        return
      }
      currentInd.value = newVal
    }

    const store = useStore()

    const { goods, target } = lazyLoad(async () =>
      Promise.resolve({
        result: await store.dispatch('home/getBrandList')
      })
    )

    return { currentInd, toggle, goods, box: target }
  }
}
</script>

<style scoped lang="less">
.home-panel {
  background: #f5f5f5;
}
.iconfont {
  width: 20px;
  height: 20px;
  background: #ccc;
  color: #fff;
  display: inline-block;
  text-align: center;
  margin-left: 5px;
  background: @xtxColor;
  &::before {
    font-size: 12px;
    position: relative;
    top: -2px;
  }
  &.disabled {
    background: #ccc;
    cursor: not-allowed;
  }
}
.box {
  display: flex;
  width: 100%;
  height: 345px;
  overflow: hidden;
  padding-bottom: 40px;
  .list {
    width: 200%;
    display: flex;
    transition: all 1s;
    li {
      margin-right: 10px;
      width: 240px;
      &:nth-child(5n) {
        margin-right: 0;
      }
      img {
        width: 240px;
        height: 305px;
      }
    }
  }
  .brandSke {
    margin-right: 10px;
    &:nth-child(5n) {
      margin-right: 0;
    }
  }
}
</style>
